const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const util = require('../../utils/util.js');
Page({
  data: {
    isfixed:false,
    listsheight:20,
    cate:'联结',
    iscate:false,
    categoryList:[],
    productList:[],
    userInfo:''

  },
  onLoad(options) {
    let _this = this
    let cate = options.cate,
    typeid = options.typeid
    console.log(typeid)
    console.log(cate)
    Controller.case({
      "apiList": `ekey=1&page=1`,
      "typeid": typeid
    }).then(res => {
      let productList = res.data.apiList[1]
      _this.setData({
        cate,
        productList
      });
      console.log(productList)
    }).catch(res=>{
  
    })
    
  },
  onShow(){
    if(!wx.getStorageSync('userInfo')){
      this.ToLogin()
    }else{
      this.setData({
        userInfo:wx.getStorageSync('userInfo')
      })
    }
  },
  ToLogin(delta) {
    var that=this;
    wx.showModal({
      title: '提示',
      content: '是否授权登录?',
      success(res) {
        console.log(res)
        if (res.confirm) {
          wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
              if(res.userInfo){
                that.update(res.userInfo)
                wx.setStorageSync('userInfo', res.userInfo)
                that.setData({
                  userInfo: res.userInfo
                })
              }
            },
            fail:(res)=>{
              wx.switchTab({
                url: '/pages/Index/Index'
              })
            }
          })
        } else if (res.cancel) {
            wx.switchTab({
              url: '/pages/Index/Index'
            })
          console.log('用户点击取消')
        }
      }
    })
  },
  selectcate(e){
    this.setData({
      cate:e.currentTarget.dataset.name,
      index:e.currentTarget.dataset.index,
      iscate:false,
    })
  },
  opencate(){
    this.setData({
      iscate:!this.data.iscate,
    })
  },
  closecate(){
    this.setData({
      cate:'',
      iscate:false,
    })
  },
  onPageScroll(event) {

    let scrollTop = event.scrollTop //页面滚动
    if (scrollTop >= this.data.listsheight) { //页面向上滚动indexKey
      this.setData({
        isfixed:true
      })
    } else { //页面向下滚动
      this.setData({
        isfixed:false
      })
    }
  },
  onShareAppMessage: function (e) {
    
  },
  onShareTimeline: function(res){
    
  },
  /**
   * 跳转列表页
   */
  jumpFootList() {
    wx.navigateTo({
      url: '/pages/footList/footList',
    })
  },
  jumpSubList(){
    wx.navigateTo({
      url: '/pages/subList/subList',
    })
  },
  jumpCollectList(){
    wx.navigateTo({
      url: '/pages/collectList/collectList',
    })
  },

    /**
   * 跳转列表页
   */
  jumpView(e) {
    util.jumpList(e)
  },
  update: function(e){
    let _this = this
    Controller.updateUserInfo({
      "head_pic": e.avatarUrl,
      "nickname": e.nickName
    }).then(res => {
  
    }).catch(res=>{
  
    })
  }
})